#include "shexiangtou.h"

uint8_t shexiangtou_jiaodu[2]={0}; //接收数据缓存区当前激光点坐标
uint8_t zxdian[10]={0};//中心点和四个角坐标
uint8_t shuzuweizhi=0; //数据位置
uint8_t zhentou=0; //云台角度接收标志
uint8_t jieshou_flag=0; //接收标志
uint8_t duqu_flag=0; //读取标志
uint8_t jiao_flag=0; //向哪一个角运动
void shexiangtou_Init(void){
    //usart2
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA3
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
    GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA

    USART_InitStructure.USART_BaudRate = 115200; //波特率115200
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
    USART_InitStructure.USART_StopBits = USART_StopBits_1; //1个停止位
    USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无流控制
    USART_Init(USART2, &USART_InitStructure); //初始化USART2

    USART_Cmd(USART2, ENABLE); //使能USART2

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置NVIC分组0
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //USART2中断通道
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级1
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断通道
    NVIC_Init(&NVIC_InitStructure); //初始化NVIC

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //使能接收中断
}

void shexiangtou_SendByte(uint8_t data)
{
    USART_SendData(USART2, data); //发送数据
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); //等待发送缓冲区空
}

void shexiangtou_SendString(uint8_t *str)
{
    while (*str) //循环发送字符串
    {
        shexiangtou_SendByte(*str++); //发送数据
    }
}

void shexiangtou_jieshou(uint8_t data){

    if(zhentou==0){
        if(data==0x50){ 
            zhentou=1; //云台角度接收标志
        }
    }else if(zhentou==1){
        if(data==0x51)zhentou=2;//固定点坐标
        else if(data==0x52)zhentou=3;//光点坐标
        else if(data==0x53)zhentou=4;//跟踪的角
        else zhentou=0;
    }else if(zhentou==3){
        shexiangtou_jiaodu[shuzuweizhi++]=data; //接收数据
        if(shuzuweizhi>=2){
            shuzuweizhi=0; //数据位置标志位
            zhentou=0; //云台角度接收标志
        }
    }else if(zhentou==2){
        zxdian[shuzuweizhi++]=data; //接收数据
        if(shuzuweizhi>=10){
            shuzuweizhi=0; //数据位置标志位
            zhentou=0; //云台角度接收标志
        }
    }else if(zhentou==4){
        jiao_flag=data; //接收数据
        zhentou=0; //云台角度接收标志
    }
}

void center(uint8_t *x,uint8_t *y){
    
    *x=zxdian[0]; //获取X坐标
    *y=zxdian[1]; //获取Y坐标
}

void jiao(uint8_t *x,uint8_t *y){
    switch (jiao_flag)
    {
    case 1:
        *x=zxdian[2]; //获取X坐标
        *y=zxdian[3]; //获取Y坐标
        break;
    case 2:
        *x=zxdian[4]; //获取X坐标
        *y=zxdian[5]; //获取Y坐标
        break;
    case 3:
        *x=zxdian[6]; //获取X坐标
        *y=zxdian[7]; //获取Y坐标
        break;
    case 4:
        *x=zxdian[8]; //获取X坐标
        *y=zxdian[9]; //获取Y坐标
        break;
    default:
        break;
    }
}

uint8_t shexiangtou_X(void){  
    return shexiangtou_jiaodu[0]; //返回X坐标
}

uint8_t shexiangtou_Y(void){
    return shexiangtou_jiaodu[1]; //返回Y坐标
}

void USART2_IRQnHandler(void) //USART2中断服务函数
{
    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
    {
        uint8_t data = USART_ReceiveData(USART2); //接收数据
        shexiangtou_jieshou(data); //处理接收到的数据
        USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除中断标志位
    }
}
